| Conditions | 1 |
| Paths | 15 |
| Total Lines | 97 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 0 | ||
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | /** |
||
| 32 | $.fn.symphonySelectable = function(options) { |
||
| 33 | var objects = this, |
||
| 34 | settings = { |
||
| 35 | items: 'tbody tr:has(input)', |
||
| 36 | ignore: 'a', |
||
| 37 | mode: 'single' |
||
| 38 | }; |
||
| 39 | |||
| 40 | $.extend(settings, options); |
||
| 41 | |||
| 42 | /*------------------------------------------------------------------------- |
||
| 43 | Events |
||
| 44 | -------------------------------------------------------------------------*/ |
||
| 45 | |||
| 46 | // Select |
||
| 47 | objects.on('click.selectable', settings.items, function select(event) { |
||
| 48 | var item = $(this), |
||
| 49 | items = item.siblings().addBack(), |
||
| 50 | object = $(event.liveFired), |
||
| 51 | target = $(event.target), |
||
| 52 | selection, deselection, first, last; |
||
| 53 | |||
| 54 | // Ignored elements |
||
| 55 | if(target.is(settings.ignore)) { |
||
| 56 | return true; |
||
| 57 | } |
||
| 58 | |||
| 59 | // Remove text ranges |
||
| 60 | if(window.getSelection) { |
||
| 61 | window.getSelection().removeAllRanges(); |
||
| 62 | } |
||
| 63 | |||
| 64 | // Range selection |
||
| 65 | if((event.shiftKey) && items.filter('.selected').length > 0 && !object.is('.single')) { |
||
| 66 | |||
| 67 | // Select upwards |
||
| 68 | if(item.prevAll().filter('.selected').length > 0) { |
||
| 69 | first = items.filter('.selected:first').index(); |
||
| 70 | last = item.index() + 1; |
||
| 71 | } |
||
| 72 | |||
| 73 | // Select downwards |
||
| 74 | else { |
||
| 75 | first = item.index(); |
||
| 76 | last = items.filter('.selected:last').index() + 1; |
||
| 77 | } |
||
| 78 | |||
| 79 | // Get selection |
||
| 80 | selection = items.slice(first, last); |
||
| 81 | |||
| 82 | // Deselect items outside the selection range |
||
| 83 | deselection = items.filter('.selected').not(selection).removeClass('selected').trigger('deselect.selectable'); |
||
| 84 | deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
| 85 | |||
| 86 | // Select range |
||
| 87 | selection.addClass('selected').trigger('select.selectable'); |
||
| 88 | selection.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
||
| 89 | } |
||
| 90 | |||
| 91 | // Single selection |
||
| 92 | else { |
||
| 93 | |||
| 94 | // Press meta or ctrl key to adjust current range, otherwise the selection will be removed |
||
| 95 | if((!event.metaKey && !event.ctrlKey && settings.mode != 'additive' && !target.is('input')) || object.is('.single')) { |
||
| 96 | deselection = items.not(item).filter('.selected').removeClass('selected').trigger('deselect.selectable'); |
||
| 97 | deselection.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
| 98 | } |
||
| 99 | |||
| 100 | // Toggle selection |
||
| 101 | if(item.is('.selected')) { |
||
| 102 | item.removeClass('selected').trigger('deselect.selectable'); |
||
| 103 | item.find('input[type="checkbox"]:hidden:first').prop('checked', false); |
||
| 104 | } |
||
| 105 | else { |
||
| 106 | item.addClass('selected').trigger('select.selectable'); |
||
| 107 | item.find('input[type="checkbox"]:hidden:first').prop('checked', true); |
||
| 108 | } |
||
| 109 | } |
||
| 110 | |||
| 111 | }); |
||
| 112 | |||
| 113 | // Remove all selections by doubleclicking the body |
||
| 114 | $('body').bind('dblclick.selectable', function removeAllSelection() { |
||
| 115 | objects.find(settings.items).removeClass('selected').trigger('deselect.selectable'); |
||
| 116 | }); |
||
| 117 | |||
| 118 | /*------------------------------------------------------------------------- |
||
| 119 | Initialisation |
||
| 120 | -------------------------------------------------------------------------*/ |
||
| 121 | |||
| 122 | // Make selectable |
||
| 123 | objects.addClass('selectable'); |
||
| 124 | |||
| 125 | /*-----------------------------------------------------------------------*/ |
||
| 126 | |||
| 127 | return objects; |
||
| 128 | }; |
||
| 129 | |||
| 131 |
This check looks for parameters in functions that are not used in the function body and are not followed by other parameters which are used inside the function.